﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Media.Imaging;

using DevEnverModal;

namespace ModalWizard
{
    public partial class MessageBox : UserControl
    {
        #region Enums
        public enum Buttons
        {
            OK,
            YesNo
        }

        public enum Icons
        {
            Information,
            Warning,
            Error
        }
        #endregion

        #region Static operations

        static MessageBox _oMessageBox;

        public static void Information(string Title, string Message)
        {
            _oMessageBox = Show(Title, Message, Buttons.OK, Icons.Information, null);
        }

        public static void Warning(string Title, string Message)
        {
            _oMessageBox = Show(Title, Message, Buttons.OK, Icons.Warning, null);
        }

        public static void Error(string Title, string Message)
        {
            _oMessageBox = Show(Title, Message, Buttons.OK, Icons.Error, null);
        }

        public static MessageBox Show(string Title, string Message, Buttons Buttons, Icons Icon, ModalClosing oCallback)
        {
            MessageBox oBox = new MessageBox(Title, Message, Buttons, Icon, oCallback);

            oBox._oModalHost.ShowModal(oBox);

            return oBox;
        }
        #endregion

        // --------------------------------------------------------------------

        ModalControl _oModalHost = new ModalControl();
        ModalClosing _oCallback;

        private MessageBox(string Title, string Message, Buttons Buttons, Icons Icon, ModalClosing oCallback)
        {
            InitializeComponent();

            _oCallback = oCallback;

            btOne.Click += OnClick;
            btTwo.Click += OnClick;
            btCross.Click += OnClick;

            txtTitle.Text = "Error";

            if (!string.IsNullOrEmpty(Title))
                txtTitle.Text = Title;

            txtMessage.Text = Message;

            if (Buttons == Buttons.YesNo)
            {
                btOne.Content = "Yes";
                btOne.Tag = ExitCode.OK;

                btTwo.Content = "No";
                btTwo.Tag = ExitCode.Cancel;
            }
            else
            {
                btOne.Visibility = Visibility.Collapsed;
                btTwo.Content = "OK";
                btTwo.Tag = ExitCode.Cancel;
            }

            btCross.Tag = ExitCode.Cancel;

            //imgInfo.Visibility = Icon == Icons.Information ? Visibility.Visible : Visibility.Collapsed;
            //imgError.Visibility = Icon == Icons.Error ? Visibility.Visible : Visibility.Collapsed;
            //imgWarning.Visibility = Icon == Icons.Warning ? Visibility.Visible : Visibility.Collapsed;

            //string sImagePath = @"Resources\error_img.png";

            //if (Icon == MsgBoxIcon.Warning)
            //    sImagePath = "/images/warning.png";
            //else if (Icon == MsgBoxIcon.Error)
            //    sImagePath = "/images/error.png";

            //BitmapImage oImage = new BitmapImage();
            //oImage.UriSource = new Uri(sImagePath);
            ////oImage.SetSource(Application.GetResourceStream(new Uri(sImagePath)).Stream);
            //_imgIcon.Source = oImage;    
        }

        void OnClick(object sender, RoutedEventArgs e)
        {
            Button btButton = (Button)sender;

            if (_oCallback == null || _oCallback(this, (ExitCode)btButton.Tag))
            {
                _oModalHost.HideModal();
                _oMessageBox = null;
            }
        }
    }
}
